//
//  NewsMainViewController.m
//  HappyTrip
//
//  Created by yuxuesong on 15-3-22.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "NewsMainViewController.h"
#import "DesCollectVC.h"
#import "HappyTripIdeasVC.h"

@interface NewsMainViewController ()

@property (nonatomic, retain) UIButton *registerButton;
@property (nonatomic, retain) UIImageView *collectImg;
@property (nonatomic, retain) UIButton *collectButton;
@property (nonatomic, retain) UIImageView *nightImg;
@property (nonatomic, retain) UIButton *nightButton;
@property (nonatomic, retain) UIImageView *cleanImg;
@property (nonatomic, retain) UIButton *cleanButton;
@property (nonatomic, retain) UIImageView *aboutImg;
@property (nonatomic, retain) UIButton *aboutButton;
@property (nonatomic, retain) UIImageView *returnImg;
@property (nonatomic, retain) UIButton *returnButton;

@property (nonatomic, retain) UIAlertView *alertView;
@property (nonatomic, assign) NSInteger temp;


@end

@implementation NewsMainViewController

- (void)dealloc
{
    [_collectImg release];
    [_nightImg release];
    [_cleanImg release];
    [_aboutImg release];
    [_returnImg release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0 / 255.0 green:170.0 / 255.0 blue:205.0 / 255.0 alpha:1.0];
    self.tabBarController.tabBar.hidden = NO;
//     self.view.backgroundColor = [UIColor colorFromHexCode:@"#F5FFFA"];
//    self.view.backgroundColor = [UIColor blackColor];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yinji.png"]];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"更多";


    
    [self setupView];
}

- (void)setupView
{


 
    UIImageView *touView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, (VIEW_HEIGHT) * 357 / 700)];
    touView.image = [UIImage imageNamed:@"tou-1.png"];
    [self.view addSubview:touView];
    
    UIImageView *weiView = [[UIImageView alloc] initWithFrame:CGRectMake(0, (VIEW_HEIGHT) * 357 / 700, VIEW_WIDTH,  (VIEW_HEIGHT) * 343 /700)];
    weiView.image = [UIImage imageNamed:@"wei-1.png"];
    [self.view addSubview:weiView];
    
    

    //我的收藏
    self.collectButton = [UIButton buttonWithTitle:nil frame:CGRectMake(0, (VIEW_HEIGHT) * 357 / 700, VIEW_WIDTH, (VIEW_HEIGHT) * 343 /2800 - 1) target:self action:@selector(collectedButtonAction:)];
    [self.collectButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:_collectButton];
//    _collectButton.backgroundColor = [UIColor colorWithRed:51.0 / 255.0 green:51.0 / 255.0 blue:102.0 / 255.0 alpha:0.8];

    
    //意见反馈
    self.nightButton = [UIButton buttonWithTitle:nil frame:CGRectMake(0,  (VIEW_HEIGHT) * 357 / 700 + (VIEW_HEIGHT) * 343 /2800, VIEW_WIDTH, (VIEW_HEIGHT) * 343 /2800 - 1) target:self action:@selector(nightButtonAction:)];
    [self.nightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:_nightButton];
//    _nightButton.backgroundColor = [UIColor colorWithRed:51.0 / 255.0 green:51.0 / 255.0 blue:102.0 / 255.0 alpha:0.8];

    
    //清除缓存
    self.cleanButton = [UIButton buttonWithTitle:nil frame:CGRectMake(0, (VIEW_HEIGHT) * 357 / 700 + (VIEW_HEIGHT) * 343 /2800 * 2, VIEW_WIDTH, (VIEW_HEIGHT) * 343 /2800 - 1) target:self action:@selector(cleanButtonAction:)];
    [self.cleanButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:_cleanButton];
//    _cleanButton.backgroundColor = [UIColor colorWithRed:51.0 / 255.0 green:51.0 / 255.0 blue:102.0 / 255.0 alpha:0.8];

    
    //关于我们
    self.aboutButton = [UIButton buttonWithTitle:nil frame:CGRectMake(0, (VIEW_HEIGHT) * 357 / 700 + (VIEW_HEIGHT) * 343 /2800 * 3, VIEW_WIDTH, (VIEW_HEIGHT) * 343 /2800 - 1) target:self action:@selector(aboutButtonAction:)];
    [self.aboutButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:_aboutButton];
//    _aboutButton.backgroundColor = [UIColor colorWithRed:51.0 / 255.0 green:51.0 / 255.0 blue:102.0 / 255.0 alpha:0.8];
 
    
}
- (void)registerButtonAction:(UIButton *)button
{


    

}

//推出收藏页面
- (void)collectedButtonAction:(UIButton *)button
{
    DesCollectVC *collectVC = [[DesCollectVC alloc] init];
    [self.navigationController pushViewController:collectVC animated:YES];
    [collectVC release];
}
//意见反馈
- (void)nightButtonAction:(UIButton *)button
{
    HappyTripIdeasVC *happyVC = [[HappyTripIdeasVC alloc] init];
    [self.navigationController pushViewController:happyVC animated:YES];
    [happyVC release];
}

// 清除缓存按钮
- (void)cleanButtonAction:(UIButton *)button
{
    dispatch_async(
        dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
                       
        NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
        NSLog(@"files :%ld",[files count]);
            self.temp = files.count;
        for (NSString *p in files) {
        NSError *error;
        NSString *path = [cachPath stringByAppendingPathComponent:p];
        if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
             }
        }
            NSLog(@"self.temp  %ld", self.temp);
        [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:self waitUntilDone:YES];});

    
}
-(void)clearCacheSuccess
{

    NSString *clear = [NSString stringWithFormat:@"清除%ld条缓存数据", self.temp];
    self.alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"清除成功" delegate:self cancelButtonTitle:clear otherButtonTitles:nil];
    [NSTimer scheduledTimerWithTimeInterval:3.0f
                                     target:self
                                   selector:@selector(dismissAlertView:)
                                   userInfo:nil
                                    repeats:NO];
    [self.alertView show];
    [_alertView release];
    NSLog(@"清理成功");
}

// alertView自动消失时间
- (void)dismissAlertView:(NSTimer*)timer {
    NSLog(@"Dismiss alert view");
    
    [self.alertView dismissWithClickedButtonIndex:0.2 animated:YES];
}
- (void)aboutButtonAction:(UIButton *)button
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"关于我们" message:@"印迹旅行,版本1.0.0" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
    [alertView release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
